<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class ImageComponent extends Component{
    var $image;
    var $image_type;

    /*  Tên hàm : load ()
     *  Tham số :
     *          - filename = *.jpg, *.gif, *.png  : image cần nén
     *  Return  :
     *          - dest  = *.jpg, *.gif, *.png  : image sau khi nén
     */
    function load($filename)
    {
        $image_info = getimagesize($filename);

        $this->image_type = $image_info[2];

        if( $this->image_type == IMAGETYPE_JPEG )
        {
            $this->image = imagecreatefromjpeg($filename);
        }
        elseif( $this->image_type == IMAGETYPE_GIF )
        {
            $this->image = imagecreatefromgif($filename);
        }
        elseif( $this->image_type == IMAGETYPE_PNG )
        {
            $this->image = imagecreatefrompng($filename);
        }
    }

    function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null)
    {
        if( $image_type == IMAGETYPE_JPEG )
        {
            imagejpeg($this->image,$filename,$compression);
        }
        elseif( $image_type == IMAGETYPE_GIF )
        {
            imagegif($this->image,$filename);
        }
        elseif( $image_type == IMAGETYPE_PNG )
        {
            imagepng($this->image,$filename);
        }
        if( $permissions != null)
        {
            chmod($filename,$permissions);
        }
    }

    function output( $image_type = IMAGETYPE_JPEG )
    {
        if( $image_type == IMAGETYPE_JPEG )
        {
            imagejpeg($this->image);
        }
        elseif( $image_type == IMAGETYPE_GIF )
        {
            imagegif($this->image);
        }
        elseif( $image_type == IMAGETYPE_PNG )
        {
            imagepng($this->image);
        }
    }

    function getWidth()
    {
         return imagesx($this->image);
    }

    function getHeight()
    {
         return imagesy($this->image);
    }

    function resizeToHeight($height)
    {
         $ratio = $height / $this->getHeight();
         $width = $this->getWidth() * $ratio;
         $this->resize($width,$height);
    }

    function resizeToWidth($width)
    {
         $ratio = $width / $this->getWidth();
         $height = $this->getheight() * $ratio;
         $this->resize($width,$height);
    }

    function scale($scale)
    {
        $width = $this->getWidth() * $scale/100;
        $height = $this->getheight() * $scale/100;
        $this->resize($width,$height);
    }

    function resize($width,$height)
    {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }


    /*  Tên hàm : compress_image (nén ảnh)
     *  Tham số :
     *          - src   = *.jpg, *.gif, *.png  : image cần nén
     *          - dest  = *.jpg, *.gif, *.png  : image sau khi nén
     *          - quality = 75                 : giảm chất lượng ảnh xuống 75%
     *  Return  :
     *          - dest  = *.jpg, *.gif, *.png  : image sau khi nén
     */
    function compress_image( $src, $dest , $quality )
    {
        $info = getimagesize($src);
        if ($info['mime'] == 'image/jpeg')
        {
            $image = imagecreatefromjpeg($src);
        }
        elseif ($info['mime'] == 'image/gif')
        {
            $image = imagecreatefromgif($src);
        }
        elseif ($info['mime'] == 'image/png')
        {
            $image = imagecreatefrompng($src);
        }
        else
        {
            die('Unknown image file format');
        }

        //compress and save file to jpg
        imagejpeg($image, $dest, $quality);

        //return destination file
        return $dest;
    }

    function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
    {
        // open the directory
        $dir = opendir( $pathToImages );

        // loop through it, looking for any/all JPG files:
        while (false !== ($fname = readdir( $dir ))) {
          // parse path for the extension
          $info = pathinfo($pathToImages . $fname);
          // continue only if this is a JPEG image
          if ( strtolower($info['extension']) == 'jpg' )
          {
            echo "Creating thumbnail for {$fname} <br />";

            // load image and get image size
            $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
            $width = imagesx( $img );
            $height = imagesy( $img );

            // calculate thumbnail size
            $new_width = $thumbWidth;
            $new_height = floor( $height * ( $thumbWidth / $width ) );

            // create a new temporary image
            $tmp_img = imagecreatetruecolor( $new_width, $new_height );

            // copy and resize old image into new image
            imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

            // save thumbnail into a file
            imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
          }
        }
        // close the directory
        closedir( $dir );
    }
    function createGallery( $pathToImages, $pathToThumbs )
    {
        echo "Creating gallery.html <br />";

        $output = "<html>";
        $output .= "<head><title>Thumbnails</title></head>";
        $output .= "<body>";
        $output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
        $output .= "<tr>";

        // open the directory
        $dir = opendir( $pathToThumbs );

        $counter = 0;
        // loop through the directory
        while (false !== ($fname = readdir($dir)))
        {
          // strip the . and .. entries out
          if ($fname != '.' && $fname != '..')
          {
            $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
            $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
            $output .= "</a></td>";

            $counter += 1;
            if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
          }
        }
        // close the directory
        closedir( $dir );

        $output .= "</tr>";
        $output .= "</table>";
        $output .= "</body>";
        $output .= "</html>";

        // open the file
        $fhandle = fopen( "gallery.html", "w" );
        // write the contents of the $output variable to the file
        fwrite( $fhandle, $output );
        // close the file
        fclose( $fhandle );
    }
}
